/**
 * 线
 * */

export default {
    data() {
        return {
        }
    },
    created() {
        this.handlersCircle = null
    },
    methods: {

        onToolCircle() {
            var isNew = true
            var sPoint, ePoint, entityCircle, drawLayer, dataSourceName

            this.handlersCircle = new Cesium.ScreenSpaceEventHandler(this.$viewer.scene.canvas)
            // left
            this.handlersCircle.setInputAction(movement => {
                var cartesian = this.getCatesian3FromPX(movement.position)
                if (cartesian && cartesian.x) {
                    if (isNew) {
                        isNew = false
                        sPoint = cartesian
                        ePoint = cartesian
                        dataSourceName = 'DrawToolCircle' + new Date().getTime()
                        drawLayer = new Cesium.CustomDataSource(dataSourceName)
                        var radiu = new Cesium.CallbackProperty(() => {
                            return Cesium.Cartesian3.distance(sPoint, ePoint)
                        }, false)

                        var entityCircle = drawLayer.entities.add({
                            eventName: 'DrawTool',
                            dataSourceName: dataSourceName,
                            position: cartesian,
                            ellipse: {
                                // 半短轴（画圆：半短轴和半长轴一致即可）
                                semiMinorAxis: radiu,
                                // 半长轴
                                semiMajorAxis: radiu,
                                // fill: false,
                                // 填充色
                                material: Cesium.Color.WHITE.withAlpha(0.2),
                                // 是否有边框
                                outline: true,
                                // 边框颜色
                                outlineColor: Cesium.Color.WHITE.withAlpha(0.8),
                                // 边框宽度
                                outlineWidth: 14,
                                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                                classificationType: Cesium.ClassificationType.BOTH
                            }

                        })
                        this.$viewer.dataSources.add(drawLayer)
                    } else {
                        ePoint = cartesian
                        isNew = true
                        this.offToolCircle()
                        this.onToolCircle()
                    }
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

            this.handlersCircle.setInputAction(movement => {
                var cartesian = this.getCatesian3FromPX(movement.endPosition)
                if (cartesian && cartesian.x) {
                    ePoint = cartesian
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // right
            this.handlersCircle.setInputAction(movement => {
                var cartesian = this.getCatesian3FromPX(movement.endPosition)
                if (cartesian && cartesian.x) {
                    ePoint = cartesian
                }
                this.offToolCircle()
                this.onToolCircle()
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
        },
        offToolCircle() {
            if (this.handlersCircle) {
                this.handlersCircle.destroy()
                this.handlersCircle = null
            }
        }
    }
}
